<template>
  <div>
    <div class="login-page">
      <!-- 顶部logo和标题 -->
      <div class="register-header">
        <el-image class="military-badge" src="" fit="cover" />
        <h1>戎归职途</h1>
        <p>贵阳贵安退役军人及随军家属就业服务平台</p>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        class="login-form"
        @submit.prevent="handleSubmit"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入手机号/身份证号"
            prefix-icon="el-icon-user"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            clearable
          />
          <div class="forget-password">
            <el-link
              type="primary"
              :underline="false"
              @click="handleForgetPassword"
            >
              忘记密码?
            </el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            native-type="submit"
            :loading="loading"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="register-link">
        还没有账号?
        <el-link type="primary" :underline="false" @click="handleRegister">
          立即注册
        </el-link>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { login } from "@/api/index";
const router = useRouter();
const loginForm = ref(null);
const loading = ref(false);

const form = ref({
  username: "",
  password: "",
});
const rules = ref({
  username: [
    { required: true, message: "请输入手机号或身份证号", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$|^\d{17}[\dXx]$/,
      message: "请输入正确的手机号或身份证号",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 20, message: "密码长度为6-20位", trigger: "blur" },
  ],
});
const handleSubmit = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      loginSubmit();
    }
  });
};
const loginSubmit = () => {
  let params = {
    uname: form.username,
    password: form.password,
    per_login_type: 0,
  };
  login(params)
    .then((res) => {
      if (res.code == 200) {
        setTimeout(() => {
          ElMessage.success(res.status_desc);
          router.push("/home");
          loading.value = false;
        }, 1500);
      } else {
      }
    })
    .catch((err) => {
      loading.value = false;
    });
};
const handleRegister = () => {
  router.push("/register");
};
</script>
<style scoped>
.login-page {
  min-height: 100vh;
  padding: 20px;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
}
.register-header {
  text-align: center;
  margin: 40px 0 30px;
}

.military-badge {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid #d4af37;
  padding: 5px;
  background-color: white;
  margin: 0 auto;
}

.register-header h1 {
  color: #4a6b2a;
  font-size: 24px;
  font-weight: bold;
  margin: 15px 0 5px;
}

.register-header p {
  color: #666;
  font-size: 14px;
}


.military-badge img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid #d4af37;
  padding: 5px;
  background-color: white;
}

.login-form {
  background-color: white;
  padding: 25px 20px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.forget-password {
  text-align: right;
  margin-top: 5px;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  background: linear-gradient(to right, #4a6b2a, #2c3e1a);
  border: none;
}

.login-btn:hover {
  opacity: 0.9;
}

.icon {
  width: 50px;
  height: 50px;
  font-size: 24px;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

/* 响应式调整 */
@media (max-width: 375px) {

  .military-badge img {
    width: 70px;
    height: 70px;
  }

  .login-form {
    padding: 20px 15px;
  }
}
</style>